<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/26
 * Time: 15:56
 */

namespace app\api\model;


class Product extends BaseModel
{

    protected $hidden = [
        'create_time','category_id','pivot','update_time','delete_time','from'
    ];

    protected function getMainImgUrlAttr($value){
        return config('img_prefix').$value;
    }


    public static function getRecentProduct($count){
        $products = self::limit($count)
            ->order('create_time desc')
            ->select();
        return $products;
    }


    public function imgs(){
        return $this->hasMany('ProductImage','product_id','id');
    }

    public function properties(){
        return $this->hasMany('ProductProperty','product_id','id');
    }

    public static function getProductByCategory($cid){
        $products = self::where('category_id',$cid)->select();
        return $products;
    }


    /**
     * @param $id
     */
    public static function findById($id){
        /*$product = self::with(['imgs'=>function($query){
                $query->with(['imgUrl'])->order('order');
            }])
            ->with('properties')
            ->find($id);*/
        $product = self::with(['imgs'=>function($query){
                $query->with(['imgUrl'])->order('order');
            },'properties'])
            ->find($id);
        return $product;
    }

}